<?php

namespace app\store\controller;

use app\api\controller\user\Bartender;
use app\store\model\User as UserModel;
use app\common\model\DeliveryImg as DeliveryImgModel;
use think\Db;
use think\Exception;
use think\Request;

/**
 * 用户管理
 * Class User
 * @package app\store\controller
 */
class User extends Controller
{
    /**
     * 用户列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new UserModel;
        //用户表
        $aparam['user_type']= 1;
        $list = $model->getList($aparam);
        return $this->fetch('index', compact('list'));
    }
    
     /**
     * 配送员列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function delivery()
    {
        $model = new UserModel;
        //用户表
        $aparam['user_type']= 2;
        $list = $model->getList($aparam);
        return $this->fetch('delivery', compact('list'));
    }

     /**
     * 待发货-指定配送员列表接口
     */
    public function delivery_list()
    {
        $model = new UserModel;
        //用户表
        $aparam['user_type']= 2;
        $aparam['status']= ["<>",2];
        #$aparam['isworking']= "N";
        $list = $model->getList($aparam);
        return $this->fetch('delivery_list', compact('list'));
    }

     /**
     * 服务员列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function bartender()
    {
        $model = new UserModel;
        //用户表
        $where['user_type']= 3;
        $whereOr= 'user_type=1 && status=1';
        $list = $model->alias("u")
            ->join("bartender b","u.user_id=b.user_id","LEFT")
            ->group("u.user_id")
            ->where($where)
            ->whereOr($whereOr)#->select();halt($list);
            ->paginate(10, false, ['query' => Request::instance()->request()]);
        return $this->fetch('bartender', compact('list'));
    }

        /**
     * 禁用商品
     * @param $goods_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($user_id)
    {
        if (!(new UserModel)->update(['status'=>2],['user_id'=>$user_id])) {
            return $this->renderError('禁用失败');
        }
        return $this->renderSuccess('禁用成功');
    }
          /**
     * 通过
     * @param $goods_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function add($user_id)
    {
        if (!(new UserModel)->update(['status'=>3],['user_id'=>$user_id])) {
            return $this->renderError('审核失败');
        }
        return $this->renderSuccess('审核/启用成功');
    }

      /**
     * 修改
     * @param $goods_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function edits($user_id,$status)
    {
        if (!(new UserModel)->update(['status'=>$status],['user_id'=>$user_id])) {
            return $this->renderError('修改失败');
        }
        return $this->renderSuccess('修改成功');
    }

      /**
     * 修改用户类型
     * @param $goods_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function appoint($user_id,$user_type,$status)
    {
        if (!(new UserModel)->update(['user_type'=>$user_type,'status'=>$status],['user_id'=>$user_id])) {
            return $this->renderError('修改失败');
        }
        return $this->renderSuccess('修改成功');
    }

       /**
     * 用户编辑
     * @param $goods_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit($user_id)
    {

        // 用户详情
        $model = (new UserModel)->detail($user_id);#halt($model);

        // 服务员等级
        if ($model["user_type"]==3 || $model["status"]==1) $level = Db::name("bartender")->where(["user_id"=>$user_id])->find();

        // 返回详情用于页面渲染：
        if (!$this->request->isPost()) {
            //图片
            $img = (new DeliveryImgModel)->where(['user_id'=>$user_id])->order(["idx"])->select();
            $name = ($model["user_type"] == 3) ? "服务员" : "配送员";
            return $this->fetch('edit', compact('model','img','name','level'));
        }

        // 更新信息：
        $data = $this->request->param();
        if (!empty($data['user'])) foreach ($data['user'] as $k => $i) {
            // 不处理空值：
            if (empty($i)) {
                unset($data['user'][$k]);
                #return $this->renderError("必填信息不可为空");
                #break;
            }
        }
        $img_model = new DeliveryImgModel();

        try {

            Db::startTrans();

            #--编辑主表信息：
            $re[] = $model::update($data['user'],["user_id"=>$data['user_id']]);
            #--编辑等级服务费信息：
            $re[] = Db::name("bartender")->where(["user_id"=>$data['user_id']])->update($data['level']);

            #--删除图片：
            $re[] = $img_model->where(["user_id"=>$data['user_id'],'d_id'=>['not in',$data['images']]])->update(['is_delete'=>1]);
            #--重新排序：
            if ($data['images']) foreach ($data["images"] as $k=>$i){
                $re[] = $img_model->where(["user_id"=>$data['user_id'],'d_id'=>$i])->update(['idx'=>$k]);
            }

            Db::commit();
            return $this->renderSuccess('更新成功', url('user/bartender'));

        } catch (Exception $e) {
            #--回滚：
            Db::rollback();
            $error = $e->getMessage() ?: '更新失败';
            return $this->renderError($error);

        }

    }

}
